package org.cttic.visualization.visualization.util.itineraryQuery;

import org.cttic.visualization.visualization.common.itineraryQueryData;
import org.cttic.visualization.visualization.util.ConvertColor;
import org.cttic.visualization.visualization.util.DateUtil;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-25 18:32
 */
@Component
public class tripUtil {

    @Autowired
    private queryUtil query;
    public void queryTrip(String vehPlate,String vehPlateColor,String queryValue1,String feeFlag,List<itineraryQueryData.Trip> list,String provinceId){
        org.cttic.visualization.visualization.common.itineraryQueryData.Trip trip = new itineraryQueryData.Trip();
        trip.setEndLat(queryValue1);
        trip.setVehPlate(vehPlate);
        trip.setVehPlateColor(ConvertColor.getColor(Integer.parseInt(vehPlateColor))+"");
        System.out.println("********按行程编号开始查询*********");
        //完成行程
        SearchHit[] hits = query.queryByTravelId(vehPlate, vehPlateColor, queryValue1,provinceId);
        if(hits.length>0) {
            System.out.println("********完成行程开始查询*********");
            //入口
            SearchHit hit = hits[0];
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            Object tansTime = sourceAsMap.get("tansTime");
            if(tansTime!=null){
                trip.setEnTime(DateUtil.timeToT(String.valueOf(tansTime)));
            }
            Object gantryId = sourceAsMap.get("gantryId");
            if(gantryId!=null){
                trip.setEnGantryId(String.valueOf(gantryId));
            }
            Object pointName = sourceAsMap.get("pointName");
            if(pointName!=null){
                trip.setEnGantryName(String.valueOf(pointName));
            }
            //出口
            SearchHit hit2 = hits[hits.length-1];
            Map<String, Object> sourceAsMap2 = hit2.getSourceAsMap();
            Object tansTime2 = sourceAsMap2.get("tansTime");
            if(tansTime2!=null){
                trip.setExitTime(DateUtil.timeToT(String.valueOf(tansTime2)));
            }
            Object gantryId2 = sourceAsMap2.get("gantryId");
            if(gantryId2!=null){
                trip.setExitGantryId(String.valueOf(gantryId2));
            }
            Object pointName2 = sourceAsMap2.get("pointName");
            if(pointName2!=null){
                trip.setExitGantryName(String.valueOf(pointName2));
            }
            trip.setIsOnline("1");
            trip.setVehStatus("0");
            //判断是否查询费用 1-查询 0-不查询
            if(feeFlag!=null&&feeFlag.equals("1")){

            }
            list.add(trip);
            System.out.println("********完成行程结束查询*********");
            //未完成行程
        }else {
            System.out.println("********未完成行程开始查询*********");
            SearchHit[] hits2 = query.queryByTravelId2(vehPlate, vehPlateColor, queryValue1,provinceId);
            if(hits2.length>0) {
                SearchHit hit = hits2[0];
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                Object tansTime = sourceAsMap.get("tansTime");
                if (tansTime != null) {
                    trip.setEnTime(DateUtil.timeToT(String.valueOf(tansTime)));
                }
                Object gantryId = sourceAsMap.get("gantryId");
                if (gantryId != null) {
                    trip.setEnGantryId(String.valueOf(gantryId));
                }
                Object pointName = sourceAsMap.get("pointName");
                if (pointName != null) {
                    trip.setEnGantryName(String.valueOf(pointName));
                }
            }
            trip.setIsOnline("0");
            trip.setVehStatus("0");
            //判断是否查询费用 1-查询 0-不查询

            if(feeFlag!=null&&feeFlag.equals("1")){

            }
            list.add(trip);
            System.out.println("********完成行程结束查询*********");
        }


    }
}
